package com.mdxl.second.controller;


import com.mdxl.second.Entity.AdminEntity;
import com.mdxl.second.Entity.CustomerEntity;
import com.mdxl.second.dao.AdminRepository;
import com.mdxl.second.dao.CustomerRepository;
import com.mdxl.second.service.CustomerService;
import com.mdxl.second.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;


@Controller
@RequestMapping(value = "/second")
public class SeondController {
    @Autowired
    CustomerRepository customerRepository;
    @Autowired
    AdminRepository adminRepository;
    @Autowired
    CustomerService customerService;
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    @ResponseBody
    public String insert(){
        CustomerEntity customerEntity =new CustomerEntity("xiaoyi","oo");
        CustomerEntity customerEntity1 =new CustomerEntity("xiaoer","enen");
        customerRepository.save(customerEntity);
        customerRepository.save(customerEntity1);

        //List<CustomerEntity> aa =customerService.findAllByFirstName("enen");
        CustomerEntity aa =customerService.findByTwo("enen");
        return aa.toString();
    }
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    @ResponseBody
    public String add(){
        //目前通过repository生成的数据库集合并不是
        AdminEntity t_admin = new AdminEntity();
        String id = String.valueOf(UUID.randomUUID());
        t_admin.setId(id);
        t_admin.setAccount("are you ok");
        t_admin.setNickName("yes my id");
        t_admin.setStatus(2);
        t_admin.setMobile("76521");
        AdminEntity a =adminRepository.save(t_admin);
        return a.toString();
    }
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    @ResponseBody
    public String get(){
        //Sort sort =new Sort(Sort.Direction.ASC,"name");
        AdminEntity adminEntity =adminRepository.findOne("31b8b9ee-5d89-44d0-af66-65b0b671f03b");
        return adminEntity.toString();
    }
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public String update(){
        //save 其实也有更新的功能
        String id ="";
        AdminEntity adminEntity =new AdminEntity();
        adminEntity.setId(id);
        //下面是关于集合的一个document的更新
        // adminEntity.set**()
        AdminEntity adminEntity1 =adminRepository.save(adminEntity);
      return adminEntity.toString();
    }
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public void delete(){
        String id ="";
        AdminEntity adminEntity =null;
        adminRepository.delete(id);
        //adminRepository.delete(adminEntity);
        //adminRepository.deleteAll();
        //adminRepository.delete(<? extends AdminEntity>);
        //总共四种删除结果
    }
    //排序和分页
    @RequestMapping(value = "/sortAndPage",method = RequestMethod.GET)
    @ResponseBody
    public String sort(){
        PageUtil pageable = new PageUtil();
        List<Order> orders = new ArrayList<Order>();
        orders.add(new Order(Sort.Direction.DESC,"createTime"));
        pageable.setSort(new Sort(orders));
        //每页显示条数
        pageable.setPagesize(2);
        //当前页
        pageable.setPageNum(0);

        Page<AdminEntity> page = adminRepository.findByAccount("cj",pageable);
        Iterator<AdminEntity> list =page.iterator();
        List<AdminEntity> list1 =new ArrayList<>();
        while (list.hasNext()){
            list1.add(list.next());
        }
        return list1.toString();
    }
}
